class NumMatrix {
public:
    vector<vector<int>> sum;
    NumMatrix(vector<vector<int>>& matrix) {
        int m = matrix.size();
        int n = matrix[0].size();
        sum.resize(m + 1, vector<int>(n + 1));
        for (int a = 0, b = 1; a < m; a++, b++)
            for (int c = 0, d = 1; c < n; c++, d++)
                sum[b][d] = matrix[a][c];

        for (int i = 1; i <= m; i++)
            for (int j = 1; j <= n; j++)
                sum[i][j] += sum[i - 1][j] +
                sum[i][j - 1] - sum[i - 1][j - 1];
    }

    int sumRegion(int row1, int col1, int row2, int col2) {
        row2++;
        col2++;
        return sum[row2][col2] - sum[row2][col1]
            - sum[row1][col2] + sum[row1][col1];
    }
};

/**
 * Your NumMatrix object will be instantiated and called as such:
 * NumMatrix* obj = new NumMatrix(matrix);
 * int param_1 = obj->sumRegion(row1,col1,row2,col2);
 */